<?php

defined('BASEPATH') or die('forbidden');

class Item_Controller Extends Controller {

    function _init() {
        check_level(50, segment(0), segment(1, 'index'));
    }

    function index() {
        set_area_list_view("flexigridarealist");
        $this->view->display("item/index");
    }

    function flexigrid() {
        //读取flexigrid传输的数据
        list($colkey, $colsinfo, $where, $sortname, $sortorder, $offset, $rp, $page) = $this->common->get_flexigrid_params();
        $rows = array();
        $sql = "SELECT COUNT(*) AS num FROM {pre}itemsample WHERE {$where}";
        $one = $this->db->one($sql);
        $total = $one['num'];
        $orderby = $sortname ? "ORDER BY {$sortname} {$sortorder} " : "";
        $this->db->query("SELECT {$colsinfo} FROM {pre}itemsample WHERE {$where} {$orderby} LIMIT {$offset},{$rp}");
        while ($n = $this->db->get_array('default')) {
            $rows[] = array('id' => $n['id'], 'cell' => ($n));
        }
        $result = array(
            'page' => $page,
            'total' => $total,
            'rows' => $rows
        );
        echo json_encode($result);
    }

    function add($id = 0) {
        //是否来源于表单请求
        if (is_submit()) {
            $rowdata = $_POST['row'];
            $one = $this->db->one("SELECT * FROM {pre}itemsample WHERE id='{$rowdata['id']}'");
            if (is_array($one))
                error(translate('C_EXISTED_PARAM_ID', $rowdata['id']));
            //更新数据表
            $id = $this->db->insert("{pre}itemsample", $rowdata);
            set_async_data($_POST['areaIds'], translate('ITEM_LOG_ADD', $rowdata['id']), __CLASS__, __FUNCTION__, $rowdata);
            insert_log(translate('ITEM'),translate('ITEM_LOG_ADD'), $rowdata);
            cp_tips("success", $id);
        }
        set_area_list_view("arealist");
        $this->view->display("item/add");
    }

    function edit($id = 0) {
        if ($id === 0)
            error(translate('C_INVALID_PARAM_ID', $id));
        $row = $this->db->one("SELECT * FROM {pre}itemsample WHERE id='{$id}'");
        if (!is_array($row))
            error(translate('C_INVALID_PARAM_ID', $id));
        //是否来源于表单请求
        if (is_submit()) {
            $rowdata = $_POST['row'];
            $one = $this->db->one("SELECT * FROM {pre}itemsample WHERE id='{$rowdata['id']}' AND id!='{$id}'");
            if (is_array($one))
                error(translate('C_EXISTED_PARAM_ID', $rowdata['id']));
            //更新数据表
            $result = $this->db->update("{pre}itemsample", $rowdata, array('id' => $id));
            insert_log(translate('ITEM'),translate('EDIT'), $row, $rowdata);
            if ($result)
                set_async_data($_POST['areaIds'], translate('ITEM_LOG_EDIT', $rowdata['id']), __CLASS__, __FUNCTION__, $rowdata);
            cp_tips("success", $id);
        }
        set_area_list_view("arealist");
        $this->view->display("item/edit", array('row' => $row));
    }

    function del($id = 0) {
        $c = 0;
        $r = '';
        if ($id === 0)
            json_output($c, translate('C_INVALID_PARAM_ID', $id));
        $row = $this->db->one("SELECT * FROM {pre}itemsample WHERE id='{$id}'");
        if (!is_array($row))
            json_output($c, translate('C_INVALID_PARAM_ID', $id));
        if ($this->db->delete('{pre}itemsample', array('id' => $id))) {
            insert_log(translate('ITEM'),translate('DEL'), array('id'=>$id));
            $c = 1;
            $r = set_async_data($_POST['areaIds'], translate('ITEM_LOG_DEL', $id), __CLASS__, __FUNCTION__, array('id' => $id), true);
        } else {
            $r = translate('C_TIPS_ERROR_TEXT');
        }
        json_output($c, $r);
    }

    function multi() {
        $type = $this->input->post("type");
        $ids = $this->input->post("ids");
        $c = 0;
        $r = '';
        $allids = $ids = explode(',', $ids);
        if (count($ids) == 0) {
            $r = translate('C_INVALID_PARAMS');
        } else {
            $ids = "'" . implode("','", $ids) . "'";
            if ($type == 'delete') {
                $this->db->query("DELETE FROM {pre}itemsample WHERE id IN ({$ids})");
                insert_log(translate('ITEM'),translate('MULTI_DEL'),array('ids'=> implode(",", $allids)));
                $c = 1;
            } else {
                $r = translate('C_INVALID_PARAMS');
            }
        }
        if ($c == 1) {
            $r = set_async_data($_POST['areaIds'], translate('ITEM_LOG_MULTI_DEL', implode(",", $allids)), __CLASS__, __FUNCTION__, array('ids' => $allids, 'type' => $type), true);
        }
        json_output($c, $r);
    }

}